import md5 from 'crypto-js/md5'
import { MessageBox } from 'element-ui'
let hashString
let duration = 60 * 1000
function version() {
  return new Promise((resolve, reject) => {
    const origin = location.origin
    const xhr = new XMLHttpRequest()
    xhr.open('GET', origin)
    xhr.responseType = 'text'
    xhr.addEventListener('readystatechange', () => {
      if (xhr.readyState === 4) {
        if (xhr.status === 200) {
          const newHsahString = md5(xhr.responseText).toString()
          if (!hashString) {
            hashString = newHsahString
            resolve()
          } else {
            if (hashString !== newHsahString) {
              MessageBox({
                title: '系统更新',
                message: `系统已更新，请及时清除缓存，刷新页面！`,
                showCancelButton: true,
                cancelButtonText: '不再提示',
                confirmButtonText: '稍后提示',
                closeOnClickModal: false,
                type: 'info',
                closeOnPressEscape: false,
                showClose: false
              }).then(() => {
                duration += 10 * 60 * 1000
                resolve()
              }).catch(() => {
                reject()
              })
            } else {
              resolve()
            }
          }
        } else {
          resolve()
        }
      }
    })
    xhr.send()
  })
}

function loop() {
  setTimeout(() => {
    version().then(() => {
      loop()
    })
  }, duration)
}
if (/^cc/.test(location.host)) {
  loop()
}
